//导入配置
const jsonServer = require('json-server')
const all = require('./API')
const { mapKeys, zipObject, keys } = require('lodash')

//###################################
// [请勿改动]整理json和router,为json-server准备
//###################################
API = mapKeys(all, (value, key) => key.replace(/\//g, '_'))
let Router = zipObject(keys(all).map(item => item + '?*'), keys(API).map(item => '/' + item))
let db = { API, Router }

//定义自己的路由
const port = 82

const server = jsonServer.create()
const router = jsonServer.router(db.API)
const middlewares = jsonServer.defaults()
const rewriter = jsonServer.rewriter(db.Router)

server.use(middlewares)
// 将 POST 请求转为 GET
server.use((request, res, next) => {
  request.method = 'GET'
  next()
})

server.use(rewriter)
server.use(router)

server.listen(port, () => console.log('API服务器：localhost:' + port))
